package com.yuhong.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuhong.reggie.entity.Category;
import com.yuhong.reggie.entity.Page;
import com.yuhong.reggie.exception.BusinessException;
import com.yuhong.reggie.mapper.CategoryMapper;
import com.yuhong.reggie.mapper.DishMapper;
import com.yuhong.reggie.mapper.SetMealMapper;
import com.yuhong.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private SetMealMapper setMealMapper;
    /**
     * @param category
     * @return
     */


    /**
     * @param category
     * @return
     */
    @Override
    public boolean save(Category category) {
        //创建时间
       category.setCreateTime(LocalDateTime.now());
       //修改时间
        category.setUpdateTime(LocalDateTime.now());

        //判断类名是否存在
        if (categoryMapper.findByName(category.getName())!=null){
            throw new BusinessException("已存在类名,请修改");
        }
        return categoryMapper.save(category)>0;
    }

    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Category> categoryList = categoryMapper.findAll();

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        Page<Category> objectPage = new Page<>();
        objectPage.setTotal(pageInfo.getTotal());
        objectPage.setRecords(pageInfo.getList());

        return objectPage;
    }

    @Override
    public boolean delete(Long id) {
        Integer dishMp = dishMapper.countByCid(id);

        //如果当前类别大于0,则不删除,提示用户
        if (dishMp>0){
            throw new BusinessException("菜品有数据,不能删除");
        }
        //如果当前类别套餐大于0,则提示
        Integer setMealMp = setMealMapper.countByCid(id);
        if (setMealMp>0){
            throw new BusinessException("套餐有数据,不能删除");
        }
        //走到这调用删除功能
        return categoryMapper.delete(id)>0;
    }

    /**
     * 修改功能
     *
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {
        Category byName2 = categoryMapper.findByName2(category);

        if (byName2!=null){
            throw new BusinessException("修改以重复,请重新修改");
        }

        category.setUpdateTime(LocalDateTime.now());
        return categoryMapper.update(category)>0;
    }

    /**
     * @return
     */
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    /**
     * 根据类型查询类别的数据
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findAllByType(Integer type) {
        return categoryMapper.findAllByType(type);
    }


}
